package com.duck.communication.controller;

import com.duck.common.domain.Result;
import com.duck.common.utils.UserContext;
import com.duck.communication.domain.dto.AddLikeDto;
import com.duck.communication.service.OperateService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@RequestMapping("/operate")
public class OperateController {

    private final OperateService operateService;

    /**
     * 点赞操作
     * @param like 入参
     * @return 状态码
     */
    @PostMapping("/addLike/")
    public Result addLike(@RequestBody AddLikeDto like){
        String userId = String.valueOf(UserContext.getUser());
        HashMap<String,Object> newLike=new HashMap<>(){{
            put("userId",userId);
            put("targetType",like.getTargetType());
            put("targetId",like.getTargetId());
        }};
        Integer result = operateService.addLike(newLike);
        if (result == 1) {
            return new Result(200,"新记录插入成功");
        } else if (result == 2) {
            return new Result(200,"已存在记录，更新 like_time");
        }
        return new Result(404,"操作失败");
    }
    /**
     * 取消点赞操作
     * @param like 入参
     * @return 状态码
     */
    @PostMapping("/cancelLike/")
    public Result cancelLike(@RequestBody AddLikeDto like){
        String userId = String.valueOf(UserContext.getUser());
        HashMap<String,Object> cancelLike=new HashMap<>(){{
            put("userId",userId);
            put("targetType",like.getTargetType());
            put("targetId",like.getTargetId());
        }};
        Integer result = operateService.cancelLike(cancelLike);
        if(result==1){
            return new Result(200,"取消成功");
        }
        return new Result(404,"你已取消过该点赞");
    }

    /**
     * 返回点赞总数
     * @param like 入参
     * @return 状态码
     */
    @PostMapping("/countLike/")
    public Result countLike(@RequestBody AddLikeDto like){
        HashMap<String,Object> countLike=new HashMap<>(){{
            put("targetType",like.getTargetType());
            put("targetId",like.getTargetId());
        }};
        Integer result = operateService.countLike(countLike);
        // 判断是否点赞，result != 0 时表示已点赞，result == 0 时表示未收藏
        Integer isLiked = result != 0 ? 1 : 0;

        // 构建返回数据
        Map<String, Object> response = new HashMap<>();
        response.put("collect_count", result);
        response.put("is_liked", isLiked);
        return new Result(response,200,"查询成功");
    }

    /**
     * 收藏操作
     * @param postId 帖子ID
     * @return 状态码
     */
    @PostMapping("/addCollect/")
    public Result addCollect(@RequestParam Integer postId){
        String userId = String.valueOf(UserContext.getUser());
        Integer result = operateService.addCollect(userId,postId);
        if (result == 1) {
            return new Result(200,"新记录插入成功");
        } else if (result == 2) {
            return new Result(200,"已存在记录，更新 collect_time");
        }
        return new Result(404,"操作失败");
    }

    /**
     * 取消收藏操作
     * @param postId 帖子ID
     * @return 状态码
     */
    @PostMapping("/cancelCollect/")
    public Result cancelCollect(@RequestParam Integer postId){
        String userId = String.valueOf(UserContext.getUser());
        Integer result = operateService.cancelCollect(userId,postId);
        if(result==1){
            return new Result(200,"取消成功");
        }
        return new Result(404,"你没有添加该收藏");
    }

    /**
     * 返回收藏总数
     * @param postId 帖子ID
     * @return 状态码
     */
    @GetMapping("/countCollect/")
    public Result countCollect(@RequestParam Integer postId){
        Integer result = operateService.selectCollectCount(postId);

        // 判断是否收藏，result != 0 时表示已收藏，result == 0 时表示未收藏
        Integer isCollected = result != 0 ? 1 : 0;

        // 构建返回数据
        Map<String, Object> response = new HashMap<>();
        response.put("collect_count", result);
        response.put("is_collected", isCollected);

        return new Result(response, 200, "查询成功");
    }

}
